package com.solution.admin.message.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.solution.admin.message.constants.MessageConstants.MessageType;
import com.solution.admin.message.constants.MessageConstants.ReceiveType;
import com.solution.admin.message.dao.MessageDao;
import com.solution.admin.message.entity.MessageEntity;
import com.solution.admin.message.service.MessageService;
import com.solution.admin.order.dao.OrderDao;
import com.solution.admin.order.dao.OrderDetailDao;
import com.solution.admin.order.entity.OrderDetailEntity;
import com.solution.admin.order.entity.OrderEntity;
import com.solution.common.constants.PublicStatus.IS_DELETE;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.common.utils.string.StringUtil;

/**
 * 消息管理service实现层
 * 
 * @author xu_cc
 * @date 2017-11-22 15:18:58
 */
@Service
public class MessageServiceImpl extends BaseServiceImpl<MessageEntity> implements MessageService {

	/**
	 * 消息管理接口
	 */
	@Autowired
	private MessageDao messageDao;

	@Autowired
	OrderDao orderDao;
	@Autowired
	OrderDetailDao orderDetailDao;

	protected BaseDao<MessageEntity> getDao() {
		return this.messageDao;
	}

	/**
	 * 发送订单消息给个人
	 * 
	 * @param orderCode
	 *            订单编码
	 * @param orderStatus
	 *            订单状态
	 * @return long
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月27日 上午10:57:10
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public long doSendOrderToUser(String orderCode, String orderStatus) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("orderCode", orderCode);
		String imagePath = null;
		OrderEntity order = orderDao.getOrderByCode(orderCode);
		// 接受人
		Long receiveId = order.getCustomerId();
		List<OrderDetailEntity> orderDetailList = orderDetailDao.listOrderDetail(param);
		if (null != orderDetailList && orderDetailList.size() > 0) {
			for (OrderDetailEntity orderDetail : orderDetailList) {
				if (StringUtil.isNotNull(orderDetail.getImagePath())) {
					imagePath = orderDetail.getImagePath();
					break;
				}
			}
		}
		MessageEntity message = new MessageEntity();
		String title = "订单";
		switch (orderStatus) {
			case "01":
				title += "待付款";
				break;
			case "02":
				title += "待审核";
				break;
			case "03":
				title += "审核驳回";
				break;
			case "04":
				title += "待发货";
				break;
			case "05":
				title += "待收货";
				break;
			case "06":
				title += "已取消";
				break;
			case "07":
				title += "已完成";
				break;
			default:
				title += "未知";
				break;
		}
		title += "！";
		message.setTitle(title);
		message.setMessageType(MessageType.TYPE_ORDER);
		message.setContent(title);
		message.setImagePath(imagePath);
		message.setReceiveType(ReceiveType.TYPE_USER);
		message.setReceiveId(receiveId);
		message.setStatus(STATUS.ENABLED.getId());
		message.setIsDelete(IS_DELETE.NORMAL.getId());
		message.setBillCode(orderCode);
		message.setSortNo(999999);
		message.setCreater("system");
		message.setModifier("system");
		return messageDao.insert(message);
	}

	/**
	 * 标识消息已读
	 * 
	 * @param param
	 * @return int
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月24日 下午3:29:40
	 */
	public int doReadMessage(Long messageId, Long userId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("messageId", messageId);
		paramMap.put("userId", userId);
		return messageDao.doReadMessage(paramMap);
	}

	/**
	 * 微信端获取用户所有信息
	 * 
	 * @param param
	 * @return List<MessageEntity>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年11月24日 下午3:05:14
	 */
	public List<MessageEntity> getWxMessageList(Integer messageType, Long userId, int limitCount) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("messageType", messageType);
		paramMap.put("userId", userId);
		paramMap.put("limitCount", limitCount);
		return messageDao.getWxMessageList(paramMap);
	}

	/**
	 * 保存消息管理
	 * 
	 * @param messageEntity
	 *            消息管理实体
	 * @return Long 添加成功消息管理的ID
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	public long saveMessage(MessageEntity messageEntity) {
		return messageDao.saveMessage(messageEntity);
	}

	/**
	 * 分页查询消息管理
	 * 
	 * @param pageParam
	 *            分页参数
	 * @param paramMap
	 *            查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	@Override
	public PageBean listMessagePage(PageParam pageParam, Map<String, Object> paramMap) {
		return messageDao.listMessagePage(pageParam, paramMap);
	}

	/**
	 * 根据ID查询用户信息
	 * 
	 * @param id
	 *            用户id
	 * @return messageEntity 用户对象
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	public MessageEntity getMessageById(Long id) {
		return messageDao.getById(id.longValue());
	}
	
	
	/**
	 * 查询最新的一条系统消息
	 * @param receiveType 消息类型 receiveType = 1
	 * @return messageEntity 单条消息对象
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	@Override
	public MessageEntity getLastMessage(String receiveType) {
		return messageDao.getLastMessage(receiveType);
	}

	/**
	 * 更新用户
	 * 
	 * @param messageEntity
	 *            用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	@Override
	public int updateMessage(MessageEntity messageEntity) {
		return messageDao.updateMessage(messageEntity);
	}

	/**
	 * 根据条件查询消息管理
	 * 
	 * @param param
	 * @return 数据列表
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	@Override
	public List<MessageEntity> listMessage(Map<String, Object> param) {
		return messageDao.listMessage(param);
	}

	/**
	 * 删除消息管理
	 * 
	 * @param
	 * @return int 返回删除记录条数
	 * @author xu_cc
	 * @date 2017-11-22 15:18:58
	 */
	public int deleteMessageById(long id) {
		return messageDao.deleteById(id);
	}

}
